package com.eeepay.eeepay_v2.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.DataDetailsActivity;
import com.eeepay.eeepay_v2.adapter.LimitAdapter;
import com.eeepay.eeepay_v2.adapter.LimitQueryAdapter;
import com.eeepay.eeepay_v2.fragment.ABBaseFragment;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.Arrays;

import cn.eeepay.api.grpc.nano.MerAddModular;
import cn.eeepay.api.grpc.nano.MerAddServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/6/29.
 */
public class LimitFragment extends ABBaseFragment {

    private static LimitFragment limitFragment;
    private View view;
    private ListView limitLv;

    private LimitQueryAdapter adapter;
//    private String bpId, merNo;//业务产品和商户ID
    private MerAddModular.MerServiceQuota[] datas;

//    public static LimitFragment getInstance(MerAddModular.MerServiceQuota[] datas) {
//        if (limitFragment == null) {
//            synchronized (LimitFragment.class) {
//                if (limitFragment == null) {
//                    limitFragment = new LimitFragment(datas);
//                }
//            }
//        }
//        return limitFragment;
//    }


    public LimitFragment() {
    }

//    public LimitFragment(MerAddModular.MerServiceQuota[] datas) {
//        this.datas = datas;
//    }

//    public LimitFragment(String bpId, String merNo) {
//        this.bpId = bpId;
//        this.merNo = merNo;
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_rate, container, false);
        initView();
        initLinstener();
        initData();
        return view;
    }


    private void initView() {
        limitLv = (ListView) view.findViewById(R.id.lv_intoQuery_rate);
    }

    private void initLinstener() {
    }

    private void initData() {
        Log.d("ching", "LimitFragment");
        datas = ((DataDetailsActivity)getActivity()).getQuotaList();
        adapter = new LimitQueryAdapter(getActivity());
        if (datas != null) {
            adapter.addAll(Arrays.asList(datas));
            limitLv.setAdapter(adapter);
        }
//        sendHttpRequest(0);
//        datas = new MerAddModular.ServiceQuota[3];
//        MerAddModular.ServiceQuota quota;
//        for (int i = 0; i < 3; i++) {
//            quota = new MerAddModular.ServiceQuota();
//            quota.serviceName = "提现";
//            quota.cardType = String.valueOf(i);
//            quota.holidaysMark = String.valueOf(i);
//            quota.serviceId = String.valueOf(i);
//            quota.singleCountAmount = "5000.00";
//            quota.singleDayAmount = "200000000.00";
//            quota.singleDaycardCount = "5000";
//            quota.singleDaycardAmount = "50000000.00";
//            datas[i] = quota;
//        }

    }

//    @Override
//    protected void sendHttpRequest(int taskId) {
//        showProgressDialog(Constant.TIP_LOADING);
//        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT
//                , taskId, new GrpcUtils.AsyncTaskListener() {
//                    @Override
//                    public Object doInBack(ManagedChannel channel, int taskId) {
//                        MerAddServiceGrpc.MerAddServiceBlockingStub stub = MerAddServiceGrpc.newBlockingStub(channel);
//                        MerAddModular.QueryMerInfoReq queryReq = new MerAddModular.QueryMerInfoReq();
//                        UserInfo userInfo = UserInfo.getInstance();
//
//                        queryReq.oneAgentNo = userInfo.getOneAgentNo();
//                        queryReq.parentNode = userInfo.getAgentNode();
//                        Log.d("saki", " bpId " + bpId + " || merchantNo" + merNo);
//                        queryReq.bpId = bpId;
//                        queryReq.merchantNo = merNo;
//                        return stub.queryMerSerQuota(queryReq);
//                    }
//
//                    @Override
//                    public void updateUI(Object result, int taskId) {
//                        dismissProgressDialog();
//                        if (result == null) {
//                            showToast("获取数据失败");
//                        } else {
//                            MerAddModular.MerServiceQuotaList response = (MerAddModular.MerServiceQuotaList) result;
//                            datas = response.merServiceQuotaList;
//                            MerAddModular.MerServiceRate rate;
////                            for (int i = 0; i < rateList.length; i++) {
////                                rate = rateList[i];
////                                Log.d("qRate", "cardType:" + rate.cardType + "|-|  cardType:" + rate.rate);
////                            }
//                            adapter.addAll(Arrays.asList(datas));
//                        }
//
//                    }
//                });
//    }

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser) {//相当于Fragment的onResume
//            if (datas == null || datas.length == 0)
//                sendHttpRequest(0);
//        } else { //相当于Fragment的onPause
//
//        }
//    }
}
